<?php
/**
 * Created by PhpStorm.
 * User: kanhai
 * Date: 2017-01-03
 * Time: 22:52
 */

namespace Api\Controller;

require_once 'vendor/autoload.php';

use Think\Controller\RestController;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;


use Think\Exception;

class BaseController extends RestController
{

    protected $addons = null;
    protected $model;
    protected $publicid;
    protected $session;
    protected $jwt_token;
    protected $token;
    protected $open_id;
    function _initialize()
    {
      if($_SERVER['HTTP_SESSION']) {
          session_id($_SERVER['HTTP_SESSION']);
          session_start();
        }
        parent::_initialize();
        if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            $this->ajaxReturn(array('msg' => '预请求', 'status' => 0, 'data' => 'NULL'));
        } else {
          if(!$this->white_list()) {
            $a     = (String) $_SERVER['HTTP_TOKEN'];
            $token = empty($a) ? I('token') : $a;

            $this->token  = $token;

            if(!$this->yz_token($token)) {
              $this->ajaxReturn(array('status' => '-99999', 'msg' => '请不要造假token', 'data' => 'null'));
            } else {
              $parse  = (new Parser())->parse($token);
              $signer = new Sha256();
              if(!($parse->verify($signer,'can_contact'))) {
                $this->ajaxReturn(array('status' => '-99999', 'msg' => '你没有权限', 'data' => 'null'));
              } else {
                $this->parse      = $parse;

                $this->uid        = $this->parse->getClaim('uid');
              }
            }
          }            
        }
    }

    // 白名单
    public function white_list($array = []) {



      $base  = '/index.php/Api/'.strtolower(CONTROLLER_NAME).'/';

      $array = empty($array) ? ['index', 'login', 'search'] : $array;


      foreach ($array as $key => $value) {
        if($_SERVER['PHP_SELF'] == $base.$value){
          return true;
          exit;
        }
      }
      return false;
    }

    // 验证token 
    public function yz_token($token) {
      //  63 184 43
      $array = explode('.', $token);

      if(count($array) != 3){
        return false;    
      } else {
        return true;
      }
    }

    // 获取图片神技能
    public function get_cover_url($data) {

     if(gettype($data) === 'array') {
       foreach($data as $k => &$v){
           if($v['cover'] && $v['cover'] != 0 || $v['slide_pic'] && $v['slide_pic'] != 0) {
               $v['cover'] = get_cover_url($v['cover'] ? $v['cover'] : $v['slide_pic']);
           }
           if($v['photos']) {
               $v['photos'] = explode(',', $v['photos']);
               foreach($v['photos'] as $k => &$v1) {
                 $v1 = get_cover_url($v1);
               }
           }

           if($v['thumb']) {
               $v['thumb'] = explode(',', $v['thumb']);
               foreach($v['thumb'] as $k => &$v1) {
                 $v1 = get_cover_url($v1);
               }
           }
       }
     }

     return $data;
    }

}
